package com.example.property.mapper;

import com.example.property.pojo.UnpaidOwner;
import com.example.property.pojo.User;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

@Repository
public interface UserMapper {
@Insert("insert into user value " +
        "(NULL, #{account}, #{password},#{idcard},#{username},#{gender},#{age},#{user_type},#{user_status},#{user_roomid},#{user_avatar})")
    int insertUser(User user);
            Page<User> owners();

//模糊查询
    @Select("<script>select userid,username,account,idcard,gender,age,user_type,user_status,user_roomid" +
            " from user where user_type = #{user_type} <if test='condition != null and condition !=\"\"'>" +
            "and (username like concat('%',#{condition},'%') " +
            "or idcard like concat('%',#{condition},'%'))</if></script>")
    Page<User> findUsersByType(@Param("condition")String condition, @Param("user_type")Integer user_type);



    @Update("update user set user_status = #{user_status} where userid = #{userid}")
    int changeUserStatus(User user);

    @Select("select userid,username,account,idcard,gender,age,user_type,user_status,user_roomid from user where userid = #{id}")
    User queryById(int id);

    @Select("select a.amount_date, a.amount,`user`.userid,`user`.username, room.room_code from " +
            "(select * from amount" + " where amount.isPaid = 2 and amount.amount_type = #{typeId}) as a" +
            " LEFT JOIN room on a.amount_roomid = room.room_id"
            + " left join user on room.room_id =`user`.user_roomid" +
            " order by a.amount_date asc ,`user`.username")

    /* 引用已经定义的 resultmap */
    @ResultMap("com.example.property.mapper.UserMapper.unpaidOwner")
    Page<UnpaidOwner> unpaidOwnerList(Integer typeId);

}


